package com.lanou3g.baidumusic.main.musicfragment.recommendfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/24.
 */

public class RecyclerViewTwoAdapter extends RecyclerView.Adapter<RecyclerViewTwoAdapter.RecyclerViewTwoHolder> {

    private RecyclerViewTwoHolder holder;
    private Context context;
    private RecommendRecyclerViewBean bean;
    private int type;

    public void setRecommendTwoRecyclerViewBean(RecommendRecyclerViewBean bean) {
        this.bean = bean;
        notifyDataSetChanged();

    }

    public RecyclerViewTwoAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    @Override
    public RecyclerViewTwoHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View view2 = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_two_rv_rv, parent, false);
        holder = new RecyclerViewTwoHolder(view2);
        return holder;

    }

    @Override
    public void onBindViewHolder(RecyclerViewTwoHolder holder, int position) {
        Log.d("RecyclerViewTwoAdapter", "type:" + type);
        switch (type) {

            case 2:
                holder.textView.setText(bean.getResult().getDiy().getResult().get(position).getTitle());
                Picasso.with(context).load(bean.getResult().getDiy().getResult().get(position).getPic()).into(holder.imageView);
                break;
            case 7:
//                for (int i = 1; i <3 ; i++) {
                Log.d("RecyclerViewTwoAdapter", "aaaaa" + position);
                Log.d("RecyclerViewTwoAdapter", "getItemCount():" + getItemCount());
//                if(position == 3) break;
                holder.textView.setText(bean.getResult().getMix_9().getResult().get(position).getTitle());
                Picasso.with(context).load(bean.getResult().getMix_9().getResult().get(position).getPic()).into(holder.imageView);
                break;

            case 9:
                holder.textView.setText(bean.getResult().getRadio().getResult().get(position).getTitle());
                Picasso.with(context).load(bean.getResult().getRadio().getResult().get(position).getPic()).into(holder.imageView);
                break;

//                }
        }

    }

    @Override
    public int getItemCount() {

        if (null == bean) {
            return 0;
        }
        if (null == bean.getResult()) {
            return 0;
        }


        switch (type) {

            case 2:
                return bean.getResult() != null && bean.getResult().getDiy().getResult().size() > 0 ? 6 : 0;
            case 7:
                return bean.getResult() != null && bean.getResult().getMix_9().getResult().size() > 0 ? 3 : 0;
            case 9:
                return bean.getResult() != null && bean.getResult().getRadio().getResult().size() > 0 ? 6 : 0;
        }

        return 0;
    }

    public class RecyclerViewTwoHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;

        public RecyclerViewTwoHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.item_fragment_recommend_two_rv_iv);
            textView = (TextView) itemView.findViewById(R.id.item_fragment_recommend_two_rv_tv);
        }
    }

}
